<template>
<jsPage
  :form="form"
  :js="js"
  :ys="ys"
  :costList="costList"
  :newJs="newJs"
  :is_hidden="is_hidden"
  :disabled="disabled"
  :is_show="is_show"
  :is_show_btn="is_show_btn"
  :btn="btn"
  @changeJs="changeJs"
  @changeFile="changeFile"
  @commit="saveProjectJs"
  ref="jsPage"
>
  <template #js1>
    <el-form-item label="项目名称">
      <el-select v-model="project" style="width:100%" @change="getJsDetail" value-key="id">
        <el-option :label="item.name" :value="item" v-for="(item,i) in project_list" :key="i"></el-option>
      </el-select>
    </el-form-item>
  </template>
</jsPage>
</template>

<script>
  import {getProjectJsSelect,getProjectJsData,saveProjectJs} from '@/apilist/Project'
  import jsPage from "./jsPage";
  import ca from "element-ui/src/locale/lang/ca";
    export default {
        name: "project_js",
        data(){
            return {
                project: '',
                rate: '',
                is_show:true,
                form: {
                    project: '',
                    partner_id: '',
                    company: '',
                    dept: '',
                    leader: '',
                    leader_mobile: '',
                    customer: '',
                    customer_mobile: '',
                    seller: '',
                    seller_mobile: '',
                    city: '',
                    start: '',
                    stop: '',
                },
                is_hidden:true,
                is_show_btn:true,
                disabled:false,
                project_list: [],
                ys: {
                    start: '',
                    stop: '',
                    cost: '',
                    offer: '0.00',
                    subtotal: '0.00',
                    total: '0.00',
                    profit: '0.00',
                    percent: '0.00',
                    tax: '0.00',
                    file_list: [
                    ],
                    seller: '李雷',
                    desc: ''
                },
                costList: [],
                js:{
                    start: '',
                    stop: '',
                    cost: '0.00',
                    offer: '0.00',
                    sub_total: '0.00',
                    total: '0.00',
                    profit: '0.00',
                    percent: '0.00',
                    tax: '0.00',
                    //附件
                    file_list: [],
                    //设计附件
                    design_list: [],
                    //活动附件
                    activity_list: [],
                    desc: ''
                },
              isSubmit: true,
              newJs: [],
              btn: "提交"
            }
        },
        components:{jsPage},

        methods:{

            //获取结算下拉
            getJsSelect(){
                getProjectJsSelect().then(res => {
                    this.project_list = res.data
                })
            },
            //获取结算数据
            getJsDetail(){
                let params = {id: this.project.id,ysid: this.project.ysid}
                getProjectJsData(params).then(res => {
                    this.form = res.data.baseInfo
                    this.ys = res.data.ys
                    this.js = res.data.js
                    this.rate = res.data.baseInfo.rate
                    this.costList = res.data.costList
                    this.filterNewJs(res.data.costList)
                })

            },
            //提交结算数据
            async saveProjectJs(is_save){
              if(!this.isSubmit) return false;
              this.isSubmit = false;
              try {
                this.js.file_list = this.$refs.jsPage.getFileListByRef("jsUpload");
                this.js.activity_list = this.$refs.jsPage.getFileListByRef("activityUpload")
                this.js.design_list = this.$refs.jsPage.getFileListByRef("designUpload")
                let params = {
                  xmid: this.project.id,
                  js: this.js,
                  costList: this.costList,
                  newJs: this.newJs,
                  is_save: is_save,
                  ysid: this.project.ysid,
                  type: 1,
                }
                //机票
                const res = await saveProjectJs(params);
                await this.$success(res.msg)
                this.isSubmit = true
                this.$router.push({
                  path: "/project_list",
                  query: {
                    status: is_save ? 0 : 1,
                    tab: 2,
                  },
                })
              }catch (e) {
                this.isSubmit = true
                Promise.reject(e)
              }

            },

            changeJs(js,new_js,cost_list){
                this.js = js
                this.newJs = new_js
                this.costList = cost_list
            },
            //修改上传文件
            changeFile(file,name){
              if(this.js.hasOwnProperty(name)){
                this.js[name].push(file);
                console.log('project_js page', this.js[name])
              }
            },

          filterNewJs(costList){
            // 否则使用原始计算逻辑
            const initItems = [
              {id:0,title:'报销',jsmoney: '0.00',yf: '0.00',desc: '报销',paytype: 3,disabled: true},
              {id:0,title:'行政',jsmoney: '0.00',yf: '0.00',desc: '行政',paytype: 0,disabled: true},
            ];

            const hasReimbursement = costList.some(item => item.paytype === 3);
            const hasAdministrative = costList.some(item => item.paytype === 0);

            this.newJs = initItems.filter(item => {
              return !(
                (item.paytype === 3 && hasReimbursement) ||
                (item.paytype === 0 && hasAdministrative)
              );
            });
          }

        },
        created() {
          this.getJsSelect()
        }
    }
</script>

<style scoped>

</style>
